package com.movecar.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.movecar.R;
import com.movecar.model.PlatMessageBean;
import com.movecar.utils.AppConfig;
import com.movecar.utils.imageloader.Constants;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * An array adapter that knows how to render views when given CustomData classes
 */
public class CustomArrayAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<PlatMessageBean> dataList = new ArrayList<PlatMessageBean>();
	private ImageLoader imageLoader = null;
	
	private Activity activity;
	private String isRelate;

	public static Map<Integer, Boolean> isSelected;

	public CustomArrayAdapter(Activity activity, List<PlatMessageBean> dataList) {
		this.activity = activity;
		this.dataList = dataList;
		mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = ImageLoader.getInstance();
		// 这儿定义isSelected这个map是记录每个listitem的状态，初始状态全部为false。
		isSelected = new HashMap<Integer, Boolean>();
		for (int i = 0; i < dataList.size(); i++) {
			isSelected.put(i, false);
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final Holder holder;

		if (convertView == null) {
			holder = new Holder();
			// Inflate the view since it does not exist
			convertView = mInflater.inflate(R.layout.custom_data_view, null);

			// Create and save off the holder in the tag so we get quick access
			// to inner fields
			// This must be done for performance reasons

			holder.img = (ImageView) convertView.findViewById(R.id.img);
			holder.choose = (CheckBox) convertView.findViewById(R.id.choose);

			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}

		// Populate the text
		imageLoader.displayImage(AppConfig.HOST + dataList.get(position).getPmLogo(), holder.img,
				Constants.image_display_options, AppConfig.animateListener);

		holder.choose.setChecked(isSelected.get(position));

		return convertView;
	}

	/** View holder for the views we need access to */
	public class Holder {
		public ImageView img;
		public CheckBox choose;
	}

}
